package com.cyber.parkingremotev2;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class AdjustActivity extends Activity {
	Communication.ComBinder mComBinder;
	CommandSystem mCommandSystem;
	Button mButtonRemoteLoad;
	Button mButtonRemoteSave;
	EditText mEditText1;
	EditText mEditText2;
	EditText mEditText3;
	EditText mEditText4;
	EditText mEditText5;
	EditText mEditText6;
	EditText mEditText7;
	EditText mEditText8;
	Button mButtonSend1;
	Button mButtonSend2;
	Button mButtonSend3;
	Button mButtonSend4;
	Button mButtonSend5;
	Button mButtonSend6;
	Button mButtonSend7;
	Button mButtonSend8;
	Button mButtonReq1;
	Button mButtonReq2;
	Button mButtonReq3;
	Button mButtonReq4;
	Button mButtonReq5;
	Button mButtonReq6;
	Button mButtonReq7;
	Button mButtonReq8;
	final int seq1 = 1;
	final int field1 = 3;
	final int seq2 = 1;
	final int field2 = 5;
	final int seq3 = 2;
	final int field3 = 3;
	final int seq4 = 4;
	final int field4 = 3;
	final int seq5 = 10;
	final int field5 = 3;
	final int seq6 = 17;
	final int field6 = 3;
	final int seq7 = 17;
	final int field7 = 5;
	final int seq8 = 18;
	final int field8 = 3;

	public void setEditTextColor(int id, int color) {
		((EditText) findViewById(id)).setTextColor(color);
	}

	public int getEditTextValue(int id) {
		int value = Integer.valueOf(((EditText) findViewById(id)).getText()
				.toString());
		((EditText) findViewById(id)).setTextColor(Color.YELLOW);
		return value;
	}

	public void setEditTextValue(int id, int value) {
		((EditText) findViewById(id)).setText(String.valueOf(value));
		((EditText) findViewById(id)).setTextColor(Color.GREEN);
	}

	public void updateTextValue(int got_seq, int got_field, int value, int seq,
			int field, int id) {
		if (seq == got_seq && field == got_field) {
			setEditTextValue(id, value);
		}
	}

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_adjust);
		mCommandSystem = new CommandSystem();
		mButtonRemoteSave = (Button) findViewById(R.id.button2);
		mButtonRemoteSave.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setEditTextColor(R.id.EditText00, Color.YELLOW);
				setEditTextColor(R.id.EditText01, Color.YELLOW);
				setEditTextColor(R.id.EditText02, Color.YELLOW);
				setEditTextColor(R.id.EditText03, Color.YELLOW);
				setEditTextColor(R.id.EditText04, Color.YELLOW);
				setEditTextColor(R.id.EditText05, Color.YELLOW);
				setEditTextColor(R.id.EditText06, Color.YELLOW);
				setEditTextColor(R.id.EditText07, Color.YELLOW);
				send(mCommandSystem.save());
			}

		});
		mEditText1 = (EditText) findViewById(R.id.EditText00);
		mEditText2 = (EditText) findViewById(R.id.EditText01);
		mEditText3 = (EditText) findViewById(R.id.EditText02);
		mEditText4 = (EditText) findViewById(R.id.EditText03);
		mEditText5 = (EditText) findViewById(R.id.EditText04);
		mEditText6 = (EditText) findViewById(R.id.EditText05);
		mEditText7 = (EditText) findViewById(R.id.EditText06);
		mEditText8 = (EditText) findViewById(R.id.EditText07);
		mButtonReq1 = (Button) findViewById(R.id.Button09);
		mButtonReq2 = (Button) findViewById(R.id.Button05);
		mButtonReq3 = (Button) findViewById(R.id.Button06);
		mButtonReq4 = (Button) findViewById(R.id.Button07);
		mButtonReq5 = (Button) findViewById(R.id.Button08);
		mButtonReq6 = (Button) findViewById(R.id.Button11);
		mButtonReq7 = (Button) findViewById(R.id.Button13);
		mButtonReq8 = (Button) findViewById(R.id.Button14);
		mButtonSend1 = (Button) findViewById(R.id.Button00);
		mButtonSend2 = (Button) findViewById(R.id.Button01);
		mButtonSend3 = (Button) findViewById(R.id.Button02);
		mButtonSend4 = (Button) findViewById(R.id.Button03);
		mButtonSend5 = (Button) findViewById(R.id.Button04);
		mButtonSend6 = (Button) findViewById(R.id.Button10);
		mButtonSend7 = (Button) findViewById(R.id.Button12);
		mButtonSend8 = (Button) findViewById(R.id.Button15);
		mButtonReq1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				send(mCommandSystem.require(seq1));

			}

		});
		mButtonReq2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				send(mCommandSystem.require(seq2));

			}

		});
		mButtonReq3.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				send(mCommandSystem.require(seq3));

			}

		});
		mButtonReq4.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				send(mCommandSystem.require(seq4));

			}

		});
		mButtonReq5.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				send(mCommandSystem.require(seq5));

			}

		});
		mButtonReq6.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				send(mCommandSystem.require(seq6));

			}

		});
		mButtonReq7.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				send(mCommandSystem.require(seq7));

			}

		});
		mButtonReq8.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				send(mCommandSystem.require(seq8));

			}

		});

		mButtonSend1.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				send(mCommandSystem.encode(seq1, field1,
						getEditTextValue(R.id.EditText00)));
			}

		});
		mButtonSend2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				send(mCommandSystem.encode(seq2, field2,
						getEditTextValue(R.id.EditText01)));
			}

		});
		mButtonSend3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				send(mCommandSystem.encode(seq3, field3,
						getEditTextValue(R.id.EditText02)));
			}

		});
		mButtonSend4.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				send(mCommandSystem.encode(seq4, field4,
						getEditTextValue(R.id.EditText03)));
			}

		});
		mButtonSend5.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				send(mCommandSystem.encode(seq5, field5,
						getEditTextValue(R.id.EditText04)));
			}

		});
		mButtonSend6.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				send(mCommandSystem.encode(seq6, field6,
						getEditTextValue(R.id.EditText05)));
			}

		});

		mButtonSend7.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				send(mCommandSystem.encode(seq7, field7,
						getEditTextValue(R.id.EditText06)));
			}

		});

		mButtonSend8.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				send(mCommandSystem.encode(seq8, field8,
						getEditTextValue(R.id.EditText07)));
			}

		});


	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_setting, menu);
		return true;
	}

	protected void onStart() {
		super.onStart();
		Intent bindComIntent = new Intent(this, NetCommunication.class);
		bindService(bindComIntent, mComConnection, Context.BIND_AUTO_CREATE);
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_control:
			Intent intent = new Intent();
			setResult(RESULT_OK, intent);
			finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	void connect(String target) {
		if (mComBinder != null)
			mComBinder.connect(target);
	}

	void send(String data) {
		if (mComBinder != null)
			mComBinder.sendData(data);
	}

	private final Handler mUiHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case ParkRemoteActivity.M_GOT_DATA: {
			}
				break;
			case FileService.M_READ_OK: {
				// FIXME

			}
				break;
			case FileService.M_WRITE_OK: {
				// FIXME
				// mText.setText("write ok");
			}
				break;
			case FileService.M_IO_ERROR: {
				// mText.setText(msg.getData().getString(FileService.M_ERROR));
			}
				break;
			case SettingActivity.M_REQUIRE: {
				int seq = msg.getData().getInt(SettingActivity.M_SEQ);
				int type = msg.getData().getInt(SettingActivity.M_TYPE);
				int value = msg.getData().getInt(SettingActivity.M_VALUE);
				updateTextValue(seq, type, value, seq1, field1, R.id.EditText00);
				updateTextValue(seq, type, value, seq2, field2, R.id.EditText01);
				updateTextValue(seq, type, value, seq3, field3, R.id.EditText02);
				updateTextValue(seq, type, value, seq4, field4, R.id.EditText03);
				updateTextValue(seq, type, value, seq5, field5, R.id.EditText04);
				updateTextValue(seq, type, value, seq6, field6, R.id.EditText05);
				updateTextValue(seq, type, value, seq7, field7, R.id.EditText06);
				updateTextValue(seq, type, value, seq8, field8, R.id.EditText07);
			}
				break;

			default:
				break;
			}
		}

	};
	private final ServiceConnection mComConnection = new ServiceConnection() {

		@Override
		public void onServiceConnected(ComponentName arg0, IBinder arg1) {
			mComBinder = (Communication.ComBinder) arg1;
			mComBinder.setUiHandler(AdjustActivity.this.mUiHandler);
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			mComBinder = null;
		}
	};

}
